// VBMcgi_global.cpp
// www.VBMcgi.org

#include "VBMcgi/VBMcgi.h"
#include "VBMcgi/VBMcgi_buildid.h"
using namespace std;
using namespace br::com::sbVB::VBLib;

// return true if the link is potentially dangerous to access 
VBLIB_DLL_API bool br::com::sbVB::VBMcgi::VBMcgi_isBadString(VBString link) 
{
	if (link == "") return false;
	VBString b = link.afterFind("..");
	if (strlen(b) > 0) return true;
	b = link.afterFind("vblib");
	if (strlen(b) > 0) return false;
	b = link.afterFind("/");
	if (strlen(b) > 0) return true;
	return false;
}

VBLIB_DLL_API void br::com::sbVB::VBMcgi::file(VBMcgi & cgi, void *p) 
{
	VBString line;
	VBString fileName = cgi.getHtmlParameter(0);
	// fileName.simpleEncrypt(0);
	ifstream file(fileName);
	if (!file)
	{
		cout << "<a href=\"http://www.vbmcgi.org\">VBMcgi</a> error: File '" 
			<< fileName << "' [" << fileName.length() << " chars) not found.<br>" << endl; 
		cout << "In html file, be sure to add filename as html parameter of VBMCGI_CALL<br>" << endl; 
		cout << "Example: VBMCGI_CALL(file,<b>fileName</b>)<br>" << endl; 
		return;
	}
	cout << "<code>" << endl;
	while (true)
	{
		file >> line;
		if (file.eof()) break;
		line.replace("<","&lt;");
		line.replace(" ","&nbsp;");
		line.replace("\t","&nbsp;&nbsp;");
		cout << line << "<br>" << endl;
	}
	cout << "</code>" << endl;
}

VBLIB_DLL_API void br::com::sbVB::VBMcgi::SSI(VBMcgi & cgi, void *p) 
{
	VBString line;
	VBString fileName = cgi.getHtmlParameter(0);
	// fileName.simpleEncrypt(0);
	ifstream file(fileName);
	if (!file)
	{
		cout << "<a href=\"http://www.vbmcgi.org\">VBMcgi</a> error: File '" 
			<< fileName << "' [" << fileName.length() << " chars) not found.<br>" << endl; 
		cout << "In html file, be sure to add filename as html parameter of VBMCGI_CALL<br>" << endl; 
		cout << "Example: VBMCGI_CALL(SSI,<b>fileName</b>)<br>" << endl; 
		return;
	}
	while (true)
	{
		file >> line;
		if (file.eof()) break;
		cout << line << endl;
	}
}

VBLIB_DLL_API void br::com::sbVB::VBMcgi::buildID()
{
	cout << "VBMcgi - www.sbvb.com.br" << endl;
	cout << "Version=" << BUILD_USER_VERSION << endl;
	cout << "BuildID=" << BUILD_ID << endl;
	cout << "Build DateTime=" << BUILD_DATETIME << endl;
}

